import { PhasedTaskApiService } from './../../api/phased-task-api.service';
import { PhasedTask, PhasedTaskParam } from './../../model/task';
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router';
import { EMPTY, Observable } from 'rxjs';
import { map, take } from 'rxjs/operators';

@Injectable()
export class PhaseTaskDetailResolverService implements Resolve<PhasedTask>{

  constructor(
    private taskApi: PhasedTaskApiService,
  ) { }
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): PhasedTask | Observable<PhasedTask> | Promise<PhasedTask> {
    const id = route.paramMap.get('id');
    if (id) {
      return this.taskApi.fetchItem(parseInt(id, 10)).pipe(
        take(1),
        map(item => new PhasedTask(item)),
      );
    }
    return EMPTY;
  }
}
